package com.zhan.ielts.web.controller;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zhan.frame.core.dao.BaseService;
import com.zhan.frame.core.web.controller.BaseControllerImpl;
import com.zhan.frame.core.web.domain.CustomResult;
import com.zhan.ielts.domain.CompositionCorrection;
import com.zhan.ielts.domain.Param;
import com.zhan.ielts.domain.query.CompositionCorrectionQuery;
import com.zhan.ielts.domain.vo.CompositionCorrectionVo;
import com.zhan.ielts.service.CompositionCorrectionService;
import com.zhan.ielts.service.ParamService;

@Controller
@RequestMapping("/composition/correction")
public class CompositionCorrectionController extends
		BaseControllerImpl<CompositionCorrection, CompositionCorrectionQuery> {

	private Logger log = LoggerFactory.getLogger(CompositionCorrectionController.class);

	@Autowired
	private CompositionCorrectionService compositionCorrectionService;

	@Autowired
	private ParamService paramService;

	@Override
	protected BaseService<CompositionCorrection> getBaseService() {
		return compositionCorrectionService;
	}

	@ResponseBody
	@RequestMapping(value = "/fetch", method = RequestMethod.POST)
	public CustomResult fetch(String userId, String verifyCode) {
		log.debug("获取作文修改信息开始");
		Map<String, Integer> map = new HashMap<String, Integer>();
		if (StringUtils.isEmpty(userId)) {
			return new CustomResult(0, "userId 为空！", map);
		}

		List<CompositionCorrection> compositionCorrectionList = null;
		try {
			CompositionCorrection query = new CompositionCorrection();
			query.setUserId(userId);
			query.setVerifyCode(verifyCode);
			compositionCorrectionList = compositionCorrectionService.fetchCompositionCorrection(query);
		} catch (Exception e) {
			log.debug("获取作文修改信息失败！");
			map.put("errCode", 1001);
			return new CustomResult(0, "系统错误, 获取T作文修改信息失败！", map);
		}
		log.debug("获取作文修改信息结束");

		return new CustomResult(1, "", compositionCorrectionList);
	}

	@ResponseBody
	@RequestMapping(value = "/updateFields", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public CustomResult updateFields(CompositionCorrection entity) {
		log.debug("修改作文批改状态开始！");
		Map map = new HashMap();
		if (StringUtils.isEmpty(entity.getVerifyCode())) {
			return new CustomResult(0, "verifyCode 为空！", map);
		} else if (StringUtils.isEmpty(entity.getIsUsed())) {
			return new CustomResult(0, "isUsed 为空！", map);
		}

		if (entity.getIsUsed().equalsIgnoreCase("Y")) {
			entity.setIsUsed("N");
			entity.setCorrectTime(null);
		} else {
			entity.setIsUsed("Y");

			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			entity.setCorrectTime(sf.format(new Date()));
		}
		compositionCorrectionService.updateByIdSelective(entity);
		log.debug("修改作文批改状态结束！");
		return new CustomResult(1, "success！", map);
	}

	@ResponseBody
	@RequestMapping(value = "/use", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public CustomResult use(String state) {
		log.debug("修改是否允许作文批改开始！");
		Map map = new HashMap();
		if (StringUtils.isEmpty(state)) {
			return new CustomResult(0, "state 为空！", map);
		}

		try {
			Param param = new Param();
			param.setType("CompositionCorrection");
			param.setParamName("switch");
			param = paramService.queryOne(param);
			param.setParamValue(state);
			param.setUpdateTime(new Timestamp(System.currentTimeMillis()));
			paramService.updateById(param);
		} catch (Exception e) {
			return new CustomResult(0, "更新失败！", map);
		}
		log.debug("修改是否允许作文批改结束！");
		if (state.equalsIgnoreCase("Y")) {
			return new CustomResult(1, "批改功能开通！", map);
		} else {
			return new CustomResult(1, "批改功能关闭！", map);
		}
	}

	@RequestMapping(value = "/teacher", method = RequestMethod.GET)
	public ModelAndView teacher(CompositionCorrectionQuery query, @PageableDefault Pageable pageable) {
		Page<CompositionCorrectionVo> page = compositionCorrectionService.queryForTeacher(query, pageable);
		ModelAndView mav = new ModelAndView("composition/correction/listTeacher", "page", page);
		mav.addObject("query", query);
		return mav;
	}


	@Override
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView selectList(CompositionCorrectionQuery query, @PageableDefault Pageable pageable) {
		Page<CompositionCorrectionVo> page = compositionCorrectionService.queryForTeacher(query, pageable);
			ModelAndView mav = new ModelAndView(path.getListViewPath(), "page", page);
		mav.addObject("query", query);
		return mav;
	}
}
